package boogui.member;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class S_SignupDialog extends JDialog {
	

	private JTextField idTextField;
	private JTextField nameTextField;
	private JTextField hpTextField_1;
	private JTextField emailTextField;
	private JPasswordField pwdField;
	private JTextField hptextField_2;
	private JComboBox hpCbx;
	private JComboBox emailCbx;
	private JComboBox yearCbx;
	private JComboBox monthCbx;
	private JComboBox dayCbx;
	private JComboBox genreCbx;
	private boolean isId = false; ///////////////회원정보 수정
	private boolean isHp = false; 
	private boolean isEm = false; 
	private JPasswordField pwdField1;
	private JPasswordField pwField;

	public S_SignupDialog(JFrame p) {		
		super(p);
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				S_SignupDialog.class.getResource("/images/user.png")));
		setModalityType(ModalityType.DOCUMENT_MODAL);
		setTitle("회원가입");
		setSize(380, 520);
		setLocationRelativeTo(null);
		setResizable(false);

		getContentPane().setLayout(new BorderLayout());
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setPreferredSize(new Dimension(10, 50));
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("가입하기");
				okButton.addActionListener(new BtnActionListener());
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("취소");
				cancelButton.addActionListener(new BtnActionListener());
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JPanel panel = new JPanel();
			getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1);
				GridBagLayout gbl_panel_1 = new GridBagLayout();
				gbl_panel_1.columnWidths = new int[] { 10, 0, 0, 0, 41, 0, 0,
						0, 0 };
				gbl_panel_1.rowHeights = new int[] { 35, 0, 20, 0, 20, 0, 20, 0, 20,
						0, 20, 0, 20, 20, 0, 20, 0, 0 };
				gbl_panel_1.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0,
						1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
				gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
						0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
						Double.MIN_VALUE };
				panel_1.setLayout(gbl_panel_1);
				{
					JLabel lblNewLabel = new JLabel("아   이   디");
					GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
					gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel.gridx = 1;
					gbc_lblNewLabel.gridy = 1;
					panel_1.add(lblNewLabel, gbc_lblNewLabel);
				}
				{
					idTextField = new JTextField();
					idTextField.setToolTipText("4자이상, 12자이하의 아이디를 입력하세요");
					GridBagConstraints gbc_idTextField = new GridBagConstraints();
					gbc_idTextField.gridwidth = 3;
					gbc_idTextField.fill = GridBagConstraints.HORIZONTAL;
					gbc_idTextField.insets = new Insets(0, 0, 5, 5);
					gbc_idTextField.gridx = 3;
					gbc_idTextField.gridy = 1;
					panel_1.add(idTextField, gbc_idTextField);
					idTextField.setMargin(new Insets(3, 3, 3, 3));
					idTextField.setColumns(10);
				}
				{
					JButton idBtn = new JButton("아이디 확인");
					GridBagConstraints gbc_idBtn = new GridBagConstraints();
					idBtn.addActionListener(new BtnActionListener());
					
					idBtn.setActionCommand("idchk");
					gbc_idBtn.fill = GridBagConstraints.HORIZONTAL;
					gbc_idBtn.insets = new Insets(0, 0, 5, 5);
					gbc_idBtn.gridx = 6;
					gbc_idBtn.gridy = 1;
					panel_1.add(idBtn, gbc_idBtn);
				}
				{
					JLabel lblNewLabel_1 = new JLabel("비 밀 번 호");
					GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
					gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_1.gridx = 1;
					gbc_lblNewLabel_1.gridy = 3;
					panel_1.add(lblNewLabel_1, gbc_lblNewLabel_1);
				}
				{
					pwdField = new JPasswordField();
					pwdField.setToolTipText("비밀번호를 입력하세요");
					GridBagConstraints gbc_pwdField = new GridBagConstraints();
					gbc_pwdField.gridwidth = 3;
					gbc_pwdField.insets = new Insets(0, 0, 5, 5);
					gbc_pwdField.fill = GridBagConstraints.HORIZONTAL;
					gbc_pwdField.gridx = 3;
					gbc_pwdField.gridy = 3;
					panel_1.add(pwdField, gbc_pwdField);
				}
				{
					JLabel lblNewLabel_7 = new JLabel("비밀번호확인");
					GridBagConstraints gbc_lblNewLabel_7 = new GridBagConstraints();
					gbc_lblNewLabel_7.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_7.gridx = 1;
					gbc_lblNewLabel_7.gridy = 5;
					panel_1.add(lblNewLabel_7, gbc_lblNewLabel_7);
				}
				{
					pwdField1 = new JPasswordField();
					GridBagConstraints gbc_pwdField1 = new GridBagConstraints();
					gbc_pwdField1.gridwidth = 3;
					gbc_pwdField1.insets = new Insets(0, 0, 5, 5);
					gbc_pwdField1.fill = GridBagConstraints.HORIZONTAL;
					gbc_pwdField1.gridx = 3;
					gbc_pwdField1.gridy = 5;
					panel_1.add(pwdField1, gbc_pwdField1);
				}
				{
					JLabel lblNewLabel_2 = new JLabel("이         름");
					GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
					gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_2.gridx = 1;
					gbc_lblNewLabel_2.gridy = 7;
					panel_1.add(lblNewLabel_2, gbc_lblNewLabel_2);
				}
				{
					nameTextField = new JTextField();
					nameTextField.setToolTipText("이름을 입력하세요");
					GridBagConstraints gbc_nameTextField = new GridBagConstraints();
					gbc_nameTextField.gridwidth = 3;
					gbc_nameTextField.fill = GridBagConstraints.HORIZONTAL;
					gbc_nameTextField.insets = new Insets(0, 0, 5, 5);
					gbc_nameTextField.gridx = 3;
					gbc_nameTextField.gridy = 7;
					panel_1.add(nameTextField, gbc_nameTextField);
					nameTextField.setMargin(new Insets(3, 3, 3, 3));
					nameTextField.setColumns(10);
				}
				{
					JLabel lblNewLabel_3 = new JLabel("핸   드   폰");
					GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
					gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_3.gridx = 1;
					gbc_lblNewLabel_3.gridy = 9;
					panel_1.add(lblNewLabel_3, gbc_lblNewLabel_3);
				}
				{
					String[] hp = { "010", "011", "016", "017", "018", "019" };
					hpCbx = new JComboBox(hp);
					GridBagConstraints gbc_hpCbx = new GridBagConstraints();
					gbc_hpCbx.fill = GridBagConstraints.HORIZONTAL;
					gbc_hpCbx.insets = new Insets(0, 0, 5, 5);
					gbc_hpCbx.gridx = 3;
					gbc_hpCbx.gridy = 9;
					panel_1.add(hpCbx, gbc_hpCbx);
				}
				{
					hpTextField_1 = new JTextField();
					GridBagConstraints gbc_hpTextField_1 = new GridBagConstraints();
					gbc_hpTextField_1.fill = GridBagConstraints.HORIZONTAL;
					gbc_hpTextField_1.insets = new Insets(0, 0, 5, 5);
					gbc_hpTextField_1.gridx = 4;
					gbc_hpTextField_1.gridy = 9;
					panel_1.add(hpTextField_1, gbc_hpTextField_1);
					hpTextField_1.setMargin(new Insets(3, 3, 3, 3));
					hpTextField_1.setColumns(10);
				}
				{
					JButton hpBtn = new JButton("핸드폰 확인");
					GridBagConstraints gbc_hpBtn = new GridBagConstraints();
					hpBtn.addActionListener(new BtnActionListener());
					{
						hptextField_2 = new JTextField();
						GridBagConstraints gbc_hptextField_2 = new GridBagConstraints();
						gbc_hptextField_2.insets = new Insets(0, 0, 5, 5);
						gbc_hptextField_2.fill = GridBagConstraints.HORIZONTAL;
						gbc_hptextField_2.gridx = 5;
						gbc_hptextField_2.gridy = 9;
						panel_1.add(hptextField_2, gbc_hptextField_2);
						hptextField_2.setColumns(10);
					}
					hpBtn.setActionCommand("hpchk");
					gbc_hpBtn.anchor = GridBagConstraints.WEST;
					gbc_hpBtn.insets = new Insets(0, 0, 5, 5);
					gbc_hpBtn.gridx = 6;
					gbc_hpBtn.gridy = 9;
					panel_1.add(hpBtn, gbc_hpBtn);

				}
				{
					JLabel lblNewLabel_4 = new JLabel("이   메   일");
					GridBagConstraints gbc_lblNewLabel_4 = new GridBagConstraints();
					gbc_lblNewLabel_4.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_4.gridx = 1;
					gbc_lblNewLabel_4.gridy = 11;
					panel_1.add(lblNewLabel_4, gbc_lblNewLabel_4);
				}
				{
					emailTextField = new JTextField();
					emailTextField.setToolTipText("이메일 주소를 입력하세요");
					GridBagConstraints gbc_emailTextField = new GridBagConstraints();
					gbc_emailTextField.gridwidth = 3;
					gbc_emailTextField.fill = GridBagConstraints.HORIZONTAL;
					gbc_emailTextField.insets = new Insets(0, 0, 5, 5);
					gbc_emailTextField.gridx = 3;
					gbc_emailTextField.gridy = 11;
					panel_1.add(emailTextField, gbc_emailTextField);
					emailTextField.setMargin(new Insets(3, 3, 3, 3));
					emailTextField.setColumns(10);
				}
				String[] email = { "직접입력", "naver.com", "hanmail.net",
						"gmail.com", "nate.com" };
				emailCbx = new JComboBox(email);
				GridBagConstraints gbc_emailCbx = new GridBagConstraints();
				gbc_emailCbx.anchor = GridBagConstraints.WEST;
				gbc_emailCbx.insets = new Insets(0, 0, 5, 5);
				gbc_emailCbx.gridx = 6;
				gbc_emailCbx.gridy = 11;
				panel_1.add(emailCbx, gbc_emailCbx);
				{
					JButton emailBtn = new JButton("이메일 확인");
					GridBagConstraints gbc_emailBtn = new GridBagConstraints();
					emailBtn.addActionListener(new BtnActionListener());
					emailBtn.setActionCommand("emailchk");
					gbc_emailBtn.fill = GridBagConstraints.HORIZONTAL;
					gbc_emailBtn.gridwidth = 2;
					gbc_emailBtn.insets = new Insets(0, 0, 5, 5);
					gbc_emailBtn.gridx = 5;
					gbc_emailBtn.gridy = 12;
					panel_1.add(emailBtn, gbc_emailBtn);
				}

				{
					JLabel lblNewLabel_5 = new JLabel("생 년 월 일");
					GridBagConstraints gbc_lblNewLabel_5 = new GridBagConstraints();
					gbc_lblNewLabel_5.insets = new Insets(0, 0, 5, 5);
					gbc_lblNewLabel_5.gridx = 1;
					gbc_lblNewLabel_5.gridy = 14;
					panel_1.add(lblNewLabel_5, gbc_lblNewLabel_5);
				}

				String[] year = new String[100];
				for (int i = 0; i < year.length; ++i) {
					year[i] = (2013 - i) + "년";
				}
				yearCbx = new JComboBox(year);
				GridBagConstraints gbc_yearCbx = new GridBagConstraints();
				gbc_yearCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_yearCbx.insets = new Insets(0, 0, 5, 5);
				gbc_yearCbx.gridx = 3;
				gbc_yearCbx.gridy = 14;
				panel_1.add(yearCbx, gbc_yearCbx);

				String[] month = new String[12];
				for (int i = 0; i < 9; ++i) {
					month[i] = "0" + (i + 1) + "월";
				}
				for (int i = 9; i < month.length; ++i) {
					month[i] = (i + 1) + "월";
				}
				monthCbx = new JComboBox(month);
				GridBagConstraints gbc_monthCbx = new GridBagConstraints();
				gbc_monthCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_monthCbx.insets = new Insets(0, 0, 5, 5);
				gbc_monthCbx.gridx = 4;
				gbc_monthCbx.gridy = 14;
				panel_1.add(monthCbx, gbc_monthCbx);

				String[] day = new String[31];
				for (int i = 0; i < 9; ++i) {
					day[i] = "0" + (i + 1) + "일";
				}
				for (int i = 9; i < day.length; ++i) {
					day[i] = (i + 1) + "일";
				}
				dayCbx = new JComboBox(day);
				GridBagConstraints gbc_dayCbx = new GridBagConstraints();
				gbc_dayCbx.anchor = GridBagConstraints.WEST;
				gbc_dayCbx.insets = new Insets(0, 0, 5, 5);
				gbc_dayCbx.gridx = 5;
				gbc_dayCbx.gridy = 14;
				panel_1.add(dayCbx, gbc_dayCbx);
				{
					JLabel lblNewLabel_6 = new JLabel("선 호 장 르");
					GridBagConstraints gbc_lblNewLabel_6 = new GridBagConstraints();
					gbc_lblNewLabel_6.insets = new Insets(0, 0, 0, 5);
					gbc_lblNewLabel_6.gridx = 1;
					gbc_lblNewLabel_6.gridy = 16;
					panel_1.add(lblNewLabel_6, gbc_lblNewLabel_6);
				}
				String[] g = { "액션", "드라마", "멜로/로맨스", "코미디", "스릴러", "공포" };
				genreCbx = new JComboBox(g);
				GridBagConstraints gbc_genreCbx = new GridBagConstraints();
				gbc_genreCbx.fill = GridBagConstraints.HORIZONTAL;
				gbc_genreCbx.gridwidth = 3;
				gbc_genreCbx.insets = new Insets(0, 0, 0, 5);
				gbc_genreCbx.gridx = 3;
				gbc_genreCbx.gridy = 16;
				panel_1.add(genreCbx, gbc_genreCbx);

			}
		}

		setVisible(true);
	}

	private class BtnActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			MemberChkService chks = new MemberChkService();
			String msg = "";
			String hp = "";
			String email1 = "";
			MemberService ms = new MemberService();
			MemberVO m = new MemberVO();
			switch (e.getActionCommand()) {
			case "idchk": // 아이디 검사
				m.setMemid(idTextField.getText());
				switch (chks.dupChkId(m)) {
				case 1:
					msg = "중복된 아이디가 있습니다.";
					idTextField.setText(null);
					idTextField.requestFocus();
					break;
				case 2:
					msg = "아이디를 입력하세요.";
					idTextField.requestFocus();
					break;
				case 3:
					msg = "4자 이상, 12자 이하로 입력하세요";
					idTextField.requestFocus();
					break;
				default:
					msg = "사용가능한 아이디 입니다.";
					pwdField.requestFocus();
					isId = true;///////////////////////회원정보수정
					break;
				}
				JOptionPane.showMessageDialog(null, msg);
				break;
			case "hpchk": // 핸드폰 검사
				hp = (String) hpCbx.getSelectedItem();
				String hp2 = hpTextField_1.getText();
				String hp3 = hptextField_2.getText();
				m.setMemhp(hp + hp2 + hp3);
				switch (chks.dupChkHp(m)) {
				case 1:
					msg = "회원 가입 된  핸드폰 번호 입니다.";
					hpTextField_1.setText(null);
					hptextField_2.setText(null);
					hpTextField_1.requestFocus();
					break;
				case 2:
					msg = "핸드폰 번호를 입력하세요.";
					hpTextField_1.requestFocus();
					break;
				case 3:
					msg = "핸드폰 번호를 정확히 입력하세요";
					hpTextField_1.requestFocus();
					break;

				default:
					msg = "핸드폰번호가 입력되었습니다.";
					emailTextField.requestFocus();
					isHp = true;///////////////////////회원정보수정
					break;
				}
				JOptionPane.showMessageDialog(null, msg);
				break;
			case "emailchk": // 이메일 검사
				email1 = (String) emailCbx.getSelectedItem();
				if (email1.equals("직접입력")) {
					m.setMememail(emailTextField.getText());
					switch (chks.dupChkEmail(m)) {
					case 1:
						msg = "회원 가입 된  이메일입니다.";
						emailTextField.setText(null);
						emailTextField.requestFocus();
						break;
					case 2:
						msg = "이메일을 입력해주세요";
						emailTextField.requestFocus();
						break;
					default:
						msg = "이메일이 입력되었습니다.";
						yearCbx.requestFocus();
						isEm = true;///////////////////////회원정보수정
						break;
					}
					JOptionPane.showMessageDialog(null, msg);
					break;
				} else {
					m.setMememail(emailTextField.getText() + "@" + email1);
					switch (chks.dupChkEmail(m)) {
					case 1:
						msg = "회원 가입 된  이메일입니다.";
						emailTextField.setText(null);
						emailTextField.requestFocus();
						break;
					case 2:
						msg = "이메일을 입력해주세요";
						emailTextField.requestFocus();
						break;
					default:
						msg = "이메일이 입력되었습니다.";
						yearCbx.requestFocus();
						isEm = true;///////////////////////회원정보수정
						break;
					}
					JOptionPane.showMessageDialog(null, msg);
				}
				break;
				
			case "OK": // 가입
				hp = (String) hpCbx.getSelectedItem();
				hp2 = hpTextField_1.getText();
				hp3 = hptextField_2.getText();
				String email = "";
				email1 = (String) emailCbx.getSelectedItem();
				String genre = (String) genreCbx.getSelectedItem();
				String birth = (String) yearCbx.getSelectedItem()
						+ (String) monthCbx.getSelectedItem()
						+ (String) dayCbx.getSelectedItem();
				if (email1.equals("직접입력")) {
					email = emailTextField.getText();
				} else {
					email = emailTextField.getText() + "@" + email1;
				}

				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < birth.length(); i++) {
					if (Character.isDigit(birth.charAt(i))) {
						sb.append(birth.charAt(i));
					}
				}
				m.setMemid(idTextField.getText());
				m.setMempwd(new String(pwdField.getPassword()));
				m.setMemname(nameTextField.getText());
				m.setMemhp(hp + hp2 + hp3);
				m.setMememail(email);
				m.setMemgenre(genre);
				m.setMembirth(sb.toString());
				
				if (m.getMemid().length() == 0 || m.getMemname().length() == 0///////////////////////회원정보수정
						|| m.getMempwd().length() == 0 || hp2.length() == 0||hp3.length() == 0 ) {
					JOptionPane.showMessageDialog(null, "정보를 전부 입력해주세요");
					idTextField.requestFocus();
				}else if(!new String(pwdField.getPassword()).equals(new String((pwdField1.getPassword())))){
					System.out.println(pwdField.getPassword());
					System.out.println(pwdField1.getPassword());
					JOptionPane.showMessageDialog(null, "비밀번호가 틀립니다. 다시한번 입력해 주세요");
					pwdField.setText(null);
					pwdField.requestFocus();
					
				}else if(isId && isHp && isEm){
					ms.createMember(m);
					setVisible(false);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, "중복 확인 부탁드립니다 제발 플리즈~");
				}
				break;
			case "Cancel": // 취소
				setVisible(false);
				dispose();
				break;
				
				
				
			}

		}

	}
	
}
